package ucd.wirelessnetworkmonitor;

import java.util.ArrayList;
import java.util.List;

import ucd.wirelessnetworkmonitor.R;

import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;

public class WiFiScanActivity extends Activity {

	private static final String TAG = "NETMON";
	
	private WifiManager wifiManager;
	private WifiReceiver wifiReceiver;
	private ListView wiFiScanListView;
	private List<ScanResult> scanResults;
	
	// Called on activity creation
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wifi_scan);
        
        // Get WifiManager, register BroadcastReceiver and start scan.
        wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        
        // Switch on Wifi
        if (!wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }

        wifiReceiver = new WifiReceiver();
        registerReceiver(wifiReceiver,
        		new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        wifiManager.startScan();
    }
    
    public void onPause() {
    	unregisterReceiver(wifiReceiver);
    	super.onPause();
    }
    
    public void onResume() {
    	registerReceiver(wifiReceiver,
        		new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    	super.onResume();
    }
    
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.wifi_scan_options_menu, menu);
		return super.onCreateOptionsMenu(menu);
    }
    
    public boolean onMenuItemSelected(int id, MenuItem item) {
    	switch(item.getItemId()) {
    	case R.id.wifi_scan_menu_refresh:
    		Toast.makeText(this, "Scanning for available networks.",
    				Toast.LENGTH_LONG).show();
    		wifiManager.startScan();
    		return true;
    	case R.id.wifi_scan_menu_about:
    		Intent intent = new Intent(this, AboutActivity.class);
    	    startActivity(intent);
    		return true;
    	default:
    		return super.onMenuItemSelected(id, item);
    	}
    }
    
    class WifiReceiver extends BroadcastReceiver {
    	private static final String TAG = "WiFiReceiver";

		public void onReceive(Context context, Intent intent) {
			Log.d(TAG, "WIFI SCAN COMPLETE");
			
			scanResults = wifiManager.getScanResults();
			ArrayList<WiFiScanRowItem> rowItems = new ArrayList<WiFiScanRowItem>();
			
			for (ScanResult result: scanResults) {
				String capabilities = result.capabilities;
				String security;

				if (capabilities.contains("WPA"))
					security = "WPA";
				else if (capabilities.contains("WEP"))
					security = "WEP";
				else
					security = "None";
				
				WiFiScanRowItem item = new WiFiScanRowItem(
						result.SSID, Integer.toString(result.level), result.BSSID, security);
	        	rowItems.add(item);
			}
	        
	        wiFiScanListView = (ListView) findViewById(R.id.wifi_scan_list_view);
	        WiFiScanListAdapter adapter = new WiFiScanListAdapter(context, rowItems);
	        wiFiScanListView.setAdapter(adapter);
		}	
    }
}
